/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.migo.utils;


import com.aliyun.oss.ClientException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author paik
 */
public class PhoneUtil {
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^[1][3-8]\\d{9}$|^([6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]([8|6])\\d{5}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static void sendPhoneCode(String code, String phone, String ip) {
//        String result = HttpClientUtils.SMSpost(SMS.SMSURL, "{\"code\":\"" + code + "\",\"phone\":\"" + phone + "\",\"ip\":\"" + ip + "\"}", SMS.SMSKEY);
        aliyunSendSms(phone,code);
    }

    public static void aliyunSendSms(String phone,String code)
    {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FosFBatGVD2ApqH6UjX", "MI2bMxEesEuw8wB4diihff0kbMeOaB");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "PowerVision");
        request.putQueryParameter("TemplateCode", "SMS_190783783");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
    }

    public static void sendInternationalPhoneCode(String code, String phone, String ip) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("code", code));
        formparams.add(new BasicNameValuePair("phone", phone));
        formparams.add(new BasicNameValuePair("ip", ip));
        String result = new HttpClientUtils().request(formparams, SMS.SMSINTERNATIONALURL);
    }
}
